<template>
  <!-- 卡片 -->
  <el-card>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加分类按钮
       row + col 栅格系统
       type="flex"开启伸缩布局
       justify 排列方式
    -->
    <el-row type="flex" justify="end">
      <el-col :span="2"
        ><el-button size="mini" type="primary" @click="dialogVisible = true"
          >添加分类</el-button
        ></el-col
      >
    </el-row>
    <!-- 添加表单分类 -->
    <el-dialog
      title="添加分类"
      :visible="dialogVisible"
      width="30%"
      @close="resetAddForm"
    >
      <el-form :model="addForm" :rules="rules" ref="addRef" label-width="80px">
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddel">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 文章分类表格渲染
       :data 关联的是表格的数据源
       prop指定列的数据字段
       label指定列的标题
       border表格边框
       stripe 斑马纹
    -->
    <el-table :data="cateList" style="width: 100%" border stripe>
      <el-table-column type="index" label="序号" width="150"></el-table-column>
      <el-table-column prop="cate_name" label="分类名称"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column label="操作">
        <!-- 访问数据 使用作用域插槽  .row 可以获取到当前行的数据 -->
        <template #default="{ row }">
          <el-button type="primary" size="small" @click="amend(row.id)"
            >修改</el-button
          >
          <el-button type="danger" size="small" @click="delCate(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="修改分类"
      :visible="editDialogVisible"
      width="30%"
      @close="closeEditDialog"
    >
      <!-- 添加的表单
      label-width="80px" 文本占的宽度-->
      <el-form
        :model="editForm"
        :rules="rules"
        label-width="80px"
        ref="dierefs"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeEditDialog">取 消</el-button>
        <el-button type="primary" @click="handelSubmitEdit">确 定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
// import { reqGetArticleCate } from '@/api/article'
import { mapState } from 'vuex'
import {
  reqAddArticleCate,
  reqDelArticle,
  reqGetCateInfoById,
  reqUpdataArticle
} from '@/api/article'
export default {
  name: 'CateGoryPage',
  created () {
    // 获取文章分类列表
    // this.getCateList()
    this.$store.dispatch('article/getCateList')
  },
  computed: {
    ...mapState('article', ['cateList'])
  },
  data () {
    return {
      editDialogVisible: false,
      dialogVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    adddel () {
      this.dialogVisible = false
    },
    resetAddForm () {
      this.$refs.addRef.resetFields()

      this.adddel()
    },
    addCate () {
      this.$refs.addRef.validate(async (boo) => {
        if (!boo) return this.$message.info('添加失败')
        const { data } = await reqAddArticleCate(this.addForm)
        if (data.code !== 0) return this.$message.error('添加失败')
        this.$message.success('添加成功')
        this.$store.dispatch('article/getCateList')
        this.adddel()
      })
    },
    // 删除
    async delCate (id) {
      if (id === 1 || id === 2) {
        return this.$message.info('管理员不允许删除前两个分类')
      }
      const str = await this.$confirm('此操作将永久删除该标题, 是否继续?', {
        type: 'warning'
      }).catch(() => {})
      if (str !== 'confirm') return this.$message.info('操作取消')
      const { data } = await reqDelArticle(id)
      if (data.code !== 0) return this.$message.error(data.message)
      this.$message.error(data.message)
      this.$store.dispatch('article/getCateList')
    },
    closeEditDialog () {
      this.editDialogVisible = false
    },
    // 修改
    async amend (id) {
      if (id === 1 || id === 2) {
        return this.$message.warning('管理员不让修改前两项')
      }
      this.editDialogVisible = true

      const { data } = await reqGetCateInfoById(id)
      if (data.code !== 0) return this.$message.error(data.message)
      this.$message.success(data.message)
      this.editForm = data.data
    },
    handelSubmitEdit () {
      this.$refs.dierefs.validate(async (boo) => {
        if (!boo) return
        const { data } = await reqUpdataArticle(this.editForm)
        if (data.code !== 0) return this.$message.error(data.message)
        this.$message.success(data.message)
        this.editDialogVisible = false
        this.$store.dispatch('article/getCateList')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
  /deep/.el-table__header-wrapper {
    .cell {
      color: #000 !important;
    }
  }
}
</style>
